package com.edz.from0507to0513apk.Accountsave.aty;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.edz.from0507to0513apk.Base.BaseActivity;
import com.edz.from0507to0513apk.R;
import com.edz.from0507to0513apk.Util.SpUtils;
import com.edz.from0507to0513apk.Util.ToastUtils;
import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;
import com.mobsandgeeks.saripaar.annotation.Password;

import java.util.List;

import butterknife.BindView;

/**
 * A login screen that offers login via email/password.
 */

public class LoginAty extends BaseActivity implements Validator.ValidationListener {
    @BindView(R.id.toolbar_login)
    Toolbar toolbar_login;

    @NotEmpty
    @BindView(R.id.phone)
    EditText et_phone;

    @NotEmpty
    @Password(min = 6)
    @BindView(R.id.password)
    EditText et_password;

    @NotEmpty
    @BindView(R.id.verifycode)
    EditText et_verifycode;

    @BindView(R.id.btn_sendmsg)
    Button btn_sendmsg;
    @BindView(R.id.email_sign_in_button)
    Button btn_sign_in;

    private Validator validator = null;
    private boolean isLogin = false;

    @Override
    protected void initView() {
        toolbar_login.setTitle("注册/登录");
    }

    @Override
    protected void initData() {
        validator = new Validator(LoginAty.this);
        validator.setValidationListener(this);
    }

    @Override
    protected void setEvent() {
        btn_sign_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                validator.validate();
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    //验证成功
    @Override
    public void onValidationSucceeded() {
        final String phone = et_phone.getText().toString();
        final String password = et_password.getText().toString();
        final String verifycode = et_verifycode.getText().toString();

        if (phone.equals("15277397109") && password.equals("asdfghjkl") && verifycode.equals
                ("2028")) {
            skipAnotherActivity(LoginAty.this, AccountSaveMainAty.class);
            finish();
            isLogin = true;
            //保存phone
            SpUtils.put(LoginAty.this, "phone", phone);
            //保存登录状态
            SpUtils.put(LoginAty.this, "isLogin", isLogin);
        } else {
            ToastUtils.showToast(LoginAty.this, "账号信息不匹配，请重新输入");
            return;
        }

        SpUtils.put(LoginAty.this, "isLogin", isLogin);
//        ToastUtils.showToast(LoginAty.this,"验证成功，可进行登录验证操作");
    }

    //验证失败
    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            }
        }
    }
}

